﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WSPay.Common.Data;
using WSPay.Domain;

namespace WSPay.Application
{
    public class ClientService : BaseService<ClientInfo>, IClientService
    {
        public ClientService(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }

        public void CreateClient(ClientInfo client)
        {
            if (client.Guid==null || client.Name == string.Empty) throw new ArgumentNullException("Guid和Name参数不能为空");

            var count=_unitOfWork.D<ClientInfo>().Query().Count(x => x.Name == client.Name);

            if (count > 0) throw new ArgumentException("Name已经存在，更换其他字符");

            _unitOfWork.D<ClientInfo>().Insert(client);

            _unitOfWork.Commit();
        }

        public void ChangeClient(ClientInfo client)
        {
            if (client.Guid == null || client.Name == string.Empty) throw new ArgumentNullException("Guid和Name参数不能为空");

            var count = _unitOfWork.D<ClientInfo>().Query().Count(x => x.Name == client.Name&&x.ID!=client.ID);

            if (count > 0) throw new ArgumentException("Name已经存在，更换其他字符");

            _unitOfWork.Commit();
        }
    }
}
